deque::operator[]


public member function
      reference operator[] ( size_type n );
const_reference operator[] ( size_type n ) const;

Access element

Returns a reference to the element at position n in the deque container.

A similar member function, deque::at, has the same behavior as this operator function, except that deque::at signals if the requested position is out of range by throwing an exception.

Parameters

n
Position of an element in the container.
Notice that the first element has a position of 0, not 1.
Member type size_type is an unsigned integral type.


Return value

The element at the specified position in the deque container.

Member types reference and const_reference are the reference types to the elements of the deque container (generally defined as T& and const T& respectively in most storage allocation models).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// deque::operator[]
#include <iostream>
#include <deque>
using namespace std;

int main ()
{
  deque<int> mydeque (10);   // 10 zero-initialized elements
  unsigned int i;

  deque<int>::size_type sz = mydeque.size();

  // assign some values:
  for (i=0; i<sz; i++) mydeque[i]=i;

  // reverse order of elements using operator[]:
  for (i=0; i<sz/2; i++)
  {
    int temp;
    temp = mydeque[sz-1-i];
    mydeque[sz-1-i]=mydeque[i];
    mydeque[i]=temp;
  }

  cout << "mydeque contains:";
  for (i=0; i<sz; i++)
    cout << " " << mydeque[i];

  cout << endl;

  return 0;
}


Output:
mydeque contains: 9 8 7 6 5 4 3 2 1 0


Complexity

Constant.

See also